package com.example.shushiyunsst.fragment.main.mine

import android.os.Bundle
import android.view.View
import android.widget.EditText
import android.widget.Toast
import androidx.appcompat.widget.Toolbar
import com.alibaba.fastjson.JSON
import com.example.library_common.base.BaseFragmentEx
import com.example.library_common.networking.RestClient
import com.example.shushiyunsst.R
import com.example.shushiyunsst.fragment.UPDATE_TYPE_USER_LOGO
import com.example.shushiyunsst.fragment.UPDATE_TYPE_USER_NAME
import com.example.shushiyunsst.fragment.getUserInfo
import com.example.wallet.db.UserInfoManager

class UpDataUSerInfoFragment :BaseFragmentEx() {

    companion object{
        fun newInstance(): UpDataUSerInfoFragment {
            val args = Bundle()
            val fragment = UpDataUSerInfoFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun getLayoutId(): Any {
         return R.layout.up_data_user_info_fragment
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val mToolbar: Toolbar =view.findViewById(R.id.mToolbar)
        mToolbar.title = "修改我的昵称"
        mToolbar.setNavigationOnClickListener { onBackPressedSupport() }
        mToolbar.inflateMenu(R.menu.up_data_user_info_menu)


        view.findViewById<View>(R.id.tv_done).setOnClickListener {
            upLoad()
        }
    }

    private fun upLoad() {
        val mEditText: EditText =rootView.findViewById(R.id.mEditText)
        val nickname = mEditText.text.toString()
        if (nickname.isEmpty()){
            Toast.makeText(_mActivity,"用户昵称不能为空",Toast.LENGTH_SHORT).show()
            return
        }
        val userInfo = UserInfoManager.userInfoManager.getUserInfoWrapper()?.userInfo
        val maps = mapOf(
            "uid" to userInfo?.uid,
            "updateType" to UPDATE_TYPE_USER_NAME,
            "valueStr" to nickname
        )
        RestClient.builder()
            .url(R.string.updateUserInfo)
            .raw(JSON.toJSONString(maps))
            .success {

                getUserInfo(_mActivity)

                onBackPressedSupport()
            }.error { _, msg ->

                Toast.makeText(_mActivity,msg,Toast.LENGTH_SHORT).show()
            }
            .failure {
                Toast.makeText(_mActivity,"发送失败",Toast.LENGTH_SHORT).show()
            }
            .build()
            .post()
    }
}